<%-- 
    Document   : index contains STUDENTS, SUBJECTS, MARKS tables clear and xml file send to server invoke code with client and uploaded file statistic report 
    Created on : 29.02.2012, 9:44:47
    Author     : Alexey Akimov <frinatella@gmail.com>
--%>

<%@page import="java.util.logging.Level"%>
<%@page import="java.util.logging.Logger"%>
<%@page import="java.sql.SQLException"%>
<%@page import="java.sql.Statement"%>
<%@page import="java.sql.Connection"%>
<%@page import="student_report.DbConnection"%>
<%@page import="java.util.Scanner"%>
<%@page import="java.util.Collection"%>
<%@page import="java.util.Enumeration"%>
<%@page import="java.util.Map"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%! 
    private int accessCount; 
%>
<%
    // You have to set your own parameters and add to project ojdbc6.jar
    DbConnection.setConnectionParameters("jdbc:oracle:thin:@localhost:1521:XE", "hr", "hr");
%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Student Report</title>
    </head>
    <body>
        <h1>Student Report</h1>
        <div style="margin-right: 50px; float: right; border-style: solid;"><div style="margin-left: 50px; margin-right: 50px;">
        <p>Local IP: <%= request.getLocalAddr() %></p>
        <p>Local name: <%= request.getLocalName() %></p>
        <p>Method: <%= request.getMethod() %></p>
        <p>Query string: <%= request.getQueryString() %></p>
        <p>Remote address: <%= request.getRemoteAddr() %></p>
        <p>Remote host: <%= request.getRemoteHost() %></p>
        <p>Remote port: <%= request.getRemotePort() %></p>
        <h4>Parameters: </h4>
        <div style="margin-left: 20px">
        <% 
            Map<String, String[]> parameterMap = request.getParameterMap();
            Enumeration<String> parameterNames = request.getParameterNames();
            String parameterName;
            while (parameterNames.hasMoreElements()) {
                parameterName = parameterNames.nextElement();
                %><p><%= parameterName %>: 
                <%
                    String[] parameterValues = parameterMap.get(parameterName);
                    for (String parameterValue : parameterValues) {
                        out.print(parameterValue + "; ");
                    }
                %></p><%
            }
        %></div></div></div>
        <div style="margin-left: 20px">
        <p>Page loading number: <%= ++accessCount %></p>
        <p><a href="StudentsList.jsp">Students List</a></p>
        <p><a href="SubjectsList.jsp">Subjects List</a></p>
        <form method="POST" action="" enctype="multipart/form-data">
            <p>Select XML for uploading: </p>
            <input type="file" name="xml_file">
            <input type="submit" name="send_file" value="Send file">
        </form>
        <br>
        <form method="POST" action="">
            <input type="submit" name="clear_db" value="Clear datebase">
        </form><br><%
            if (parameterMap.containsKey("clear_db")) {
                try {
                Connection connection = DbConnection.getConnection();
                connection.createStatement().executeUpdate("begin "
                        + "     delete from MARKS;"
                        + "     delete from STUDENTS;"
                        + "     delete from SUBJECTS;"
                        + "end;");
                    out.print("<h3>Delete succeded</h3>");  
                } catch (SQLException ex) {
                    out.print("<h3>Delete failed</h3>" + ex.getMessage());
                }
            }
            %>
            <p><b>Content type: </b><%= request.getContentType() %></p>
            <%
                if (request.getContentType() != null && request.getContentType().contains("multipart/form-data")) {
                    Part[] parts = request.getParts().toArray(new Part[0]);
                    for (Part part : parts) {
                        %>
                            <p><b>Name:</b> <%= part.getName() %></p>
                            <div style="margin-left: 20px">
                            <p>Size: <%= part.getSize() %></p>
                            <p>Content type: <%= part.getContentType() %></p>
                        <%
                        if ("xml_file".equals(part.getName())) {
                            if ("text/xml".equals(part.getContentType())) {
                                if (part.getSize() < 512000) {
                                    String fileContent = new Scanner(part.getInputStream()).useDelimiter("\\A").next();
                                    %><textarea><%= fileContent %></textarea><%
                                    // Here you can invoke your xml parser
                                } else {
                                    out.print("Maximum file size (500KB) has been exceeded.");
                                }
                            } else {
                                out.print("Not XML has been loaded.");
                            }
                        } else {
                            out.print("O_o");
                        }
                        out.print("</div>");
                    }
                }
        %>
    </div></body>
</html>
